<template>
  <!-- 对话框组件：动态标题；双向绑定对话框是否显示；固定对话框宽度为1200px；关闭时销毁对话框内组件（避免状态残留）；关闭前的回调 -->
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="1200px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('projectPlanName')" prop="projectPlanFK" :error="error.projectPlanFK">
            <el-select v-model="form.projectPlanFK" clearable filterable :placeholder="$t('inputprojectPlanName')">
              <el-option v-for="item in projectPlanOption" :key="item.project_name" :label="item.project_name" :value="item.id">
                <span style="float: left">{{ item.project_name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteType')" prop="quote_type" :error="error.quote_type">
            <el-select v-model="form.quote_type" :placeholder="$t('inputquoteType')">
              <el-option
                v-for="status in QuoteTypeOption"
                :key="status.value"
                :label="status.label"
                :value="status.value"
              >
                <span style="float: left">{{ status.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteNo')" prop="quote_no" :error="error.quote_no">
            <el-input v-model="form.quote_no" maxlength="10" show-word-limit :placeholder="$t('inputquoteNo')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteWFNo')" prop="quote_wfno" :error="error.quote_wfno">
            <el-input v-model="form.quote_wfno" maxlength="10" show-word-limit :placeholder="$t('inputquoteWFNo')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteDate')" prop="quote_date" :error="error.quote_date">
            <el-date-picker v-model="form.quote_date" type="date" value-format="yyyy-MM-dd" :placeholder="$t('inputquoteDate')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteContent')" prop="quote_content" :error="error.quote_content">
            <el-input v-model="form.quote_content" maxlength="50" show-word-limit :placeholder="$t('inputquoteContent')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteAmount')" prop="quote_amount" :error="error.quote_amount">
            <el-input v-model="form.quote_amount" :placeholder="$t('inputquoteAmount')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteCurrency')" prop="quote_curr" :error="error.quote_curr">
            <el-select v-model="form.quote_curr" clearable allow-create filterable :placeholder="$t('inputquoteCurrency')">
              <el-option v-for="status in currencyOptions" :key="status.currency" :label="status.currency" :value="status.currency">
                <span style="float: left">{{ status.currency }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ status.currency_name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteDays')" prop="quote_days" :error="error.quote_days">
            <el-input v-model="form.quote_days" :placeholder="$t('inputquoteDays')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('receiveDate')" prop="receive_date" :error="error.receive_date">
            <el-date-picker v-model="form.receive_date" type="date" value-format="yyyy-MM-dd" :placeholder="$t('inputreceiveDate')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('quoteNote')" prop="note" :error="error.note">
            <el-input v-model="form.note" maxlength="150" show-word-limit :placeholder="$t('inputquoteNote')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" :disabled="!isModified" @click="handleCreate">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" :disabled="!isModified" @click="handleUpdate">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import sheetMixin from '@/mixins/SheetMixin'
import { createProjectQutaApi, updateProjectQutaApi } from '../api/projectquota.api';
import { getCodeTableDetailApi } from '@/api/common/common'
import { getProjectPlanBaseListApi } from '@/views/projectbase/projectplanbase/api/projectplanbase.api'
import { getCurrencyListApi } from '@/views/master/currency/api/currency.api';

export default {
  mixins: [sheetMixin],
  data() {
    return {
      formRef: 'form',
      createTitle: this.$t('projectQuotaCreateTitle'),
      updateTitle: this.$t('projectQuotaUpdateTitle'),
      menuOptions: [],
      projectPlanOption: [],
      QuoteTypeOption: [],
      currencyOptions: [],
      actionApi: {
        'create': createProjectQutaApi,
        'update': updateProjectQutaApi
      },
      form: {
        projectPlanFK: undefined,
        quote_no: undefined,
        quote_wfno: undefined,
        quote_date: undefined,
        quote_content: undefined,
        quote_amount: undefined,
        quote_curr: undefined,
        quote_days: undefined,
        receive_date: undefined,
        quote_type: undefined,
        note: undefined
      },
      error: {
        projectPlanFK: undefined,
        quote_no: undefined,
        quote_wfno: undefined,
        quote_date: undefined,
        quote_content: undefined,
        quote_amount: undefined,
        quote_curr: undefined,
        quote_days: undefined,
        receive_date: undefined,
        quote_type: undefined,
        note: undefined
      },
      rules: {
        projectPlanFK: [{ required: true, message: this.$t('projectRequired'), trigger: 'blur' }],
        quote_no: [{ required: true, message: this.$t('quoteNoRequired'), trigger: 'blur' }],
        quote_amount: [{ required: true, message: this.$t('quoteAmountRequired'), trigger: 'blur' }],
        quote_curr: [{ required: true, message: this.$t('quoteCurrencyRequired'), trigger: 'blur' }],
        quote_days: [{ required: true, message: this.$t('quoteDaysRequired'), trigger: 'blur' }],
        quote_date: [{ required: true, message: this.$t('quoteDateRequired'), trigger: 'blur' }]
      }
    };
  },
  computed: {
    labelWidth() {
      return this.$i18n.locale === 'en' ? '150px' : '100px';
    }
  },
  mounted() {
    // 获取Codetable下拉框数据
    getCodeTableDetailApi('ProjectQuotaModel', 'quote_type').then(resp => {
      this.QuoteTypeOption = resp.data;
    });
  },
  methods: {
    initialize() {
      this.getProjectPlanOption()
      this.getCurrncyList()
    },
    // 从币种主表中获取币种
    async getCurrncyList() {
      var resp = await getCurrencyListApi()
      if (resp.success) {
        this.currencyOptions = resp.data.results;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    // 获取项目计划List
    async getProjectPlanOption(query) {
      var resp = await getProjectPlanBaseListApi({ search: query, page: 1, page_size: 20 })
      if (resp.success) {
        this.projectPlanOption = resp.data.results;
      } else {
        this.projectPlanOption = [];
      }
    },
    handleCreate() {
      this.submitForm('create', this.$t('createSuccess', { title: this.$t('quoteNo'), value: this.form.quote_no }))
    },
    handleUpdate() {
      this.submitForm('update', this.$t('updateSuccess', { title: this.$t('quoteNo'), value: this.form.quote_no }))
    }
  }
};
</script>

<style scoped>
.el-input input, .table-select .el-input__inner {
    width: 220px;
    box-sizing: border-box;
}
::v-deep .el-form-item__label {
    text-align: right;
}
</style>
